<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/13
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Driver;
use think\Db;
use think\Config;



/**
 * 二手车店家🛻🏬
 */
class CarStore extends Driver
{

    protected $noNeedLogin = ['get_all_used_car'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        // $user = $this->auth->getUserinfo();

        if($this->auth->group_id<2){
            $this->error('您是货主角色');
        }
        
    }

    /**
     * 完善二手车店铺信息🛻
     *
     * @param string $name 二手车店铺名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 二手车店铺logo图片
     * @param string $images banner图
     * @param string $description 简介
     * @return void
     */
    public function reg_car_store($name,$lng,$lat,$addr,$tel,$contacts,$logo_image,$images,$description)
    {
      
        $data['uid'] = $this->auth->id;
        $car_store = Db::name('car_store')->where($data)->find();
        if($car_store){
            //第二角色:0=无,1=加油站,2=二手车卖家,3=车辆维修
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'2']);
            $this->error('您已注册二手车店铺');
        }
        //判断是否有其他的店铺角色
        if($this->auth->rule_status>0){
            $this->error('您已有其他的店铺');
        }

        $data['name'] = $name;
        $data['lng'] = $lng;
        $data['lat'] = $lat;
        $data['addr'] = $addr;

        $data['tel'] = $tel;
        $data['contacts'] = $contacts;
        $data['logo_image'] = $logo_image;
        $data['images'] = $images;
        $data['description'] = $description;

        $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['createtime'] = time();

        $id = Db::name('car_store')->insertGetId($data);
        if($id){
      
            $this->UpdateGeoCarStore($lng,$lat,$id);//更新二手车店铺定位
          
            //修改附属角色
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'1']);
            $this->success('注册成功');
        }

    }

    

    /**
     * 编辑二手车店铺信息🛻
     *
     * @param string $name 二手车店铺名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 二手车店铺logo图片
     * @param string $images banner图
     * @param string $description 简介
     * 
     * @return void
     */
    public function edit_car_store($name = null ,$lng = null ,$lat = null ,$addr = null ,$tel = null ,$contacts = null ,$logo_image = null ,$images = null ,$description=null)
    {
        $where['uid'] = $this->auth->id;
        $car_store = Db::name('car_store')->where($where)->find();
        if(!$car_store){
            $this->error('您没有二手车店铺');
        }elseif($car_store['switch']!=1){
            $this->error('您的二手车店铺已被关闭，请联系平台');
        }

        $data['name'] = $name;
        $data['lng'] = $lng;
        $data['lat'] = $lat;
        $data['addr'] = $addr;

        $data['tel'] = $tel;
        $data['contacts'] = $contacts;
        $data['logo_image'] = $logo_image;
        $data['images'] = $images;
        $data['description'] = $description;


        // $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['updatetime'] = time();

        $res = Db::name('car_store')->where($where)->update($data);


        $this->UpdateGeoCarStore($lng,$lat,$car_store['id']);//更新二手车店铺定位

        if($res){
            $this->success('修改成功');
        }else{
            $this->error('未修改内容');
        }


    }



    /**
     * 获取我的二手车店铺信息🛻
     *
     * @return void
     */
    public function get_my_car_store_data()
    {
     
        $car_store = $this->GetCarStoreDataByUid($this->auth->id);
        // if(!$car_store){
        //     $this->error('您没有二手车店铺');
        // }
        $this->success('ok',$car_store);
    }



    /**
     * 发布二手车🛻
     *
     * @param string $name 名称
     * @param string $cartype_id 车辆类型id
     * @param string $long 车长
     * @param string $images 车辆照片
     * @param string $image 主图
     * @param string $hp 马力
     * @param string $engine_brand 发动机品牌
     * @param string $discharge 排放标准
     * @param string $licensing_date 上牌日期
     * @param string $reg_addr 注册地点
     * @param string $coach_type 车厢类型
     * @param string $car_insurance 车险情况
     * @param string $upshift 是否能提档
     * @param string $ps 备注
     * @param string $description 简介
     * @param string $carbrand_id 车辆品牌id
     * @param string $price 价格万元
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @return void
     */
    public function add_used_car($name = null,
                                $cartype_id = null,
                                $long = null,
                                $images = null,
                                $image = null,
                                $hp = null,
                                $engine_brand = null,
                                $discharge = null,
                                $licensing_date = null,
                                $reg_addr = null,
                                $coach_type = null,
                                $car_insurance = null,
                                $upshift = null,
                                $ps = null,
                                $description = null,
                                $carbrand_id = null,
                                $price = null,
                                $tel = null,
                                $contacts = null){

        $car_store = $this->GetCarStoreDataByUid($this->auth->id);


        $data['car_store_id'] = $car_store['id'];
        $data['uid'] = $this->auth->id;
        $data['name'] = $name;
        $data['cartype_id'] = $cartype_id;
        $data['long'] = $long;

        $data['images'] = $images;
        $data['image'] = $image;
        $data['hp'] = $hp;

        $data['engine_brand'] = $engine_brand;
        $data['discharge'] = $discharge;
        $data['licensing_date'] = $licensing_date;

        $data['reg_addr'] = $reg_addr;
        $data['coach_type'] = $coach_type;
        $data['car_insurance'] = $car_insurance;

        $data['upshift'] = $upshift;
        $data['ps'] = $ps;
        $data['description'] = $description;

        $data['carbrand_id'] = $carbrand_id;
        $data['price'] = $price;
        $data['tel'] = $tel;
        $data['contacts'] = $contacts;




        $used_car = Db::name('used_car')->where($data)->find();

        if($used_car){
            $this->error('您已发布该二手车');
        }
  
        Db::name('used_car')->insert($data);
        $this->success('添加二手车成功');
    }


    /**
     * 编辑二手车🛻
     *
     * @param string $id 二手车id
     * @param string $name 名称
     * @param string $cartype_id 车辆类型id
     * @param string $long 车长
     * @param string $images 车辆照片
     * @param string $image 主图
     * @param string $hp 马力
     * @param string $engine_brand 发动机品牌
     * @param string $discharge 排放标准
     * @param string $licensing_date 上牌日期
     * @param string $reg_addr 注册地点
     * @param string $coach_type 车厢类型
     * @param string $car_insurance 车险情况
     * @param string $upshift 是否能提档
     * @param string $ps 备注
     * @param string $description 简介
     * @param string $carbrand_id 车辆品牌id
     * @param string $price 价格万元
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @return void
     */
    public function edit_used_car($id,$name = null,
                                $cartype_id = null,
                                $long = null,
                                $images = null,
                                $image = null,
                                $hp = null,
                                $engine_brand = null,
                                $discharge = null,
                                $licensing_date = null,
                                $reg_addr = null,
                                $coach_type = null,
                                $car_insurance = null,
                                $upshift = null,
                                $ps = null,
                                $description = null,
                                $carbrand_id = null,
                                $price = null,
                                $tel = null,
                                $contacts = null){

        $car_store = $this->GetCarStoreDataByUid($this->auth->id);


        $data['car_store_id'] = $car_store['id'];

        $data['name'] = $name;
        $data['cartype_id'] = $cartype_id;
        $data['long'] = $long;

        $data['images'] = $images;
        $data['image'] = $image;
        $data['hp'] = $hp;

        $data['engine_brand'] = $engine_brand;
        $data['discharge'] = $discharge;
        $data['licensing_date'] = $licensing_date;

        $data['reg_addr'] = $reg_addr;
        $data['coach_type'] = $coach_type;
        $data['car_insurance'] = $car_insurance;

        $data['upshift'] = $upshift;
        $data['ps'] = $ps;
        $data['description'] = $description;

        $data['carbrand_id'] = $carbrand_id;
        $data['price'] = $price;
        $data['tel'] = $tel;
        $data['contacts'] = $contacts;


        $where['uid'] = $this->auth->id;
        $where['id'] = $id;
                
        $used_car = Db::name('used_car')->where($where)->find();

        if(!$used_car){
            $this->error('您没有该二手车');
        }

  
        $res = Db::name('used_car')->where($where)->update($data);
        $this->success('编辑二手车成功',$res);
    }

    /**
     * 下架二手车🛻不显示
     *
     * @param int $id 二手车id
     * @return void
     */
    public function no_show($id)
    {

        $where['uid'] = $this->auth->id;
        $where['id'] = $id;
                
        $used_car = Db::name('used_car')->where($where)->find();

        if(!$used_car){
            $this->error('您没有该二手车');
        }
  
        $res = Db::name('used_car')->where($where)->update(['switch'=>0]);
        $this->success('下架成功',$res);
    }



    /**
     * 上架二手车🛻显示
     *
     * @param int $id 二手车id
     * @return void
     */
    public function show($id)
    {

        $where['uid'] = $this->auth->id;
        $where['id'] = $id;
                
        $used_car = Db::name('used_car')->where($where)->find();

        if(!$used_car){
            $this->error('您没有该二手车');
        }
  
        $res = Db::name('used_car')->where($where)->update(['switch'=>1]);
        $this->success('上架成功',$res);
    }


    /**
     * 删除二手车🛻
     *
     * @param int $used_car_id 二手车id
     * @return void
     */
    public function del_used_car($used_car_id)
    {
     
        $data['id'] = $used_car_id;

        $res = Db::name('used_car')->where($data)->find();
        if(!$res){
            $this->error('没有该二手车');
        }

        $car_store = $this->GetCarStoreDataByUid($this->auth->id);
        $data['car_store_id'] = $car_store['id'];
        $res = Db::name('used_car')->where($data)->delete();
        $this->success('删除二手车成功',$res);
    }



}